from django.shortcuts import render
from django.http import HttpResponse
from common.models import Users
from datetime import datetime

# Create your views here.
def index(request):
    #浏览user信息
    list = Users.objects.all()
    context = {"userslist":list}
    return render(request,"administrator/users/index.html",context)

def add(request):
    #增加user信息
    return render(request,"administrator/users/add.html")

def insert(request):
    #执行添加user
    try:
        ob = Users()
        ob.username = request.POST['username']
        ob.name = request.POST['name']
        #获取密码并md5
        import hashlib
        m = hashlib.md5() 
        m.update(bytes(request.POST['password'],encoding="utf8"))
        ob.passwd = m.hexdigest()
        ob.sex = request.POST['sex']
        ob.address = request.POST['address']
        ob.viplevel = int(request.POST['viplevel'])
        ob.phone = request.POST['phone']
        print(request.POST['phone'])
        ob.email = request.POST['email']
        ob.identity = request.POST['identity']
        ob.registertime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        print(ob.toDict())
        context = {'info':'添加成功！'}

    except Exception as e:
        context = {"info":"添加失败！"}

    return render(request,"administrator/info.html",context)

def delete(request,uid):
    #删除user信息
    try:
        ob = Users.objects.get(id=uid)
        ob.delete()
        context = {'info':'删除成功！'}
    except:
        context = {'info':'删除失败！'}
    return render(request,"administrator/info.html",context)
def edit(request,uid):
    #编辑user信息
    print("uid ="+ uid)
    ob = Users.objects.get(id=uid)
    context = {'info':ob}
    return render(request,"administrator/users/edit.html",context)

def update(request,uid):
    #执行编辑user信息
    try:
        ob = Users.objects.get(id=uid)
        ob.name = request.POST['name']
        ob.sex = request.POST['sex']
        ob.address = request.POST['address']
        ob.viplevel = request.POST['viplevel']
        ob.phone = request.POST['phone']
        ob.email = request.POST['email']
        ob.identity = request.POST['identity']
        ob.save()
        context = {'info':'修改成功！'}
    except Exception as err:
        print(err)
        context = {'info':'修改失败！'}
    return render(request,"administrator/info.html",context)

def password(request,uid):
    ob = Users.objects.get(id=uid)
    context = {'info':ob}
    return render(request,"administrator/users/password.html",context)

def changepasswd(request):
    print("pid="+request.POST["pid"])
    try:
        if request.POST['password'] != request.POST['repassword']:
            context = {"info":"错误！输入的密码不一致！"}
            return render(request,"administrator/info.html",context)
        print("----------------------------")
        ob = Users.objects.get(id=request.POST['pid'])
        #获取密码并md5
        import hashlib
        m = hashlib.md5() 
        m.update(bytes(request.POST['password'],encoding="utf8"))
        ob.passwd = m.hexdigest()
        ob.save()
        print(ob.toDict())
        context = {'info':'添加成功！'}

    except Exception as e:
        context = {"info":"添加失败！"}

    return render(request,"administrator/info.html",context)


